// 全局函数
export function validateMobile(str) {
  // 检查手机号码格式
  return /^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16[6-7])|(17[1-8])|(18[0-9])|(19[1|3])|(19[5|6])|(19[8|9]))\d{8}$/.test(
    str
  );
}
export function validateEmail(str) {
  // 检查手机号码格式
  return /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/.test(str);
}
export function validatePhone(str) {
  // 检查手机号码格式
  return /^(0\d{2,4}-)?\d{8}$/.test(str);
}
// import moment from 'moment';
import moment from "moment";

export function formatDate(str, tpl = "YYYY-MM-DD HH:mm:ss") {
  // 返回格式化后的字符串, str: Date类型数据
  return moment(str).format(tpl);
}

export const debounce = function (fn, duration = 1000) {
  // 防抖函数
  let timer = null;
  return function () {
    if (!timer) {
      fn.call(this, ...arguments);
      timer = setTimeout(() => {
        clearTimeout(timer);
        timer = null;
      }, duration);
    } else {
      clearTimeout(timer);
      timer = setTimeout(() => {
        clearTimeout(timer);
        timer = null;
      }, duration);
    }
  };
};
export const throttle = function (fn, duration = 1000) {
  // 节流函数
  let last;
  let timer = null;
  let now;
  return function () {
    if (timer === null) {
      last = new Date().getTime();
      timer = setTimeout(() => {
        timer = null;
        fn.call(this, ...arguments);
      }, duration);
    } else {
      now = new Date().getTime();
      if (now - last <= duration) {
        clearTimeout(timer);
        last = now;
        timer = setTimeout(() => {
          timer = null;
          fn.call(this, ...arguments);
        }, duration);
      }
    }
  };
};

export function fixedDecimal(v) {
  // 为数字保留两位小数
  if (isNaN(v)) {
    return "";
  }
  if (/^\d+$/.test(v)) {
    return v + ".00";
  }
  return parseFloat(v).toFixed(2);
}

export function validateSmsCode(str) {
  // 检查验证码格式
  return /^\d4$/.test(str);
}

import CryptoJS from "crypto-js";
export function md5(data) {
  return CryptoJS.MD5(data).toString();
}

export const hash = (function () {
  const hashMap = [];
  function beforeHash() {
    const lowerCase = [];
    const upperCase = [];
    for (let i = 97; i < 123; i++) {
      lowerCase.push(String.fromCharCode(i));
    }
    for (let i = 65; i < 91; i++) {
      upperCase.push(String.fromCharCode(i));
    }
    const strs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].concat(
      lowerCase,
      upperCase,
      new Date().getTime().toString().split("")
    );
    // eslint-disable-next-line no-unused-vars
    function calculte(len) {
      return new Array(len)
        .fill()
        .map(() => strs[Math.floor(Math.random() * 63)])
        .join("");
    }

    return function (len) {
      let res = calculte(len);
      while (hashMap.includes(res)) {
        res = calculte(len);
      }
      hashMap.push(res);
      return res;
    };
  }
  return beforeHash();
})();

import Cookie from "js-cookie";
export function getToken(k = process.env.MAIN_TOKEN) {
  if (!k) {
    return;
  }
  return Cookie.get(
    (process.env.BUILD_ENV + "_" + k).toUpperCase().replace("PROD_", "")
  );
}
export function setToken(k, v, expires = 1) {
  if (!k || !v) {
    return;
  }
  return Cookie.set(
    (process.env.BUILD_ENV + "_" + k).toUpperCase().replace("PROD_", ""),
    v,
    {
      expires,
    }
  );
}
export function removeToken(k = process.env.MAIN_TOKEN) {
  return Cookie.remove(
    (process.env.BUILD_ENV + "_" + k).toUpperCase().replace("PROD_", "")
  );
}

import http from "./http";
export { http };
// 公共接口

export function centsToYuan(v) {
  if (isNaN(v)) {
    return "";
  }
  v = v / 100;
  if (/^\d+$/.test(v)) {
    return v + ".00";
  }
  if (v.toString().toFixed) {
    return v.toString().toFixed(2);
  } else {
    return String(v);
  }
}

export function yuanToCents(v) {
  if (isNaN(v)) {
    return "";
  }
  return parseInt(v * 100 + 0.5);
}

export function toTop(v) {
  if (isNaN(v)) {
    return;
  }
  // Chrome
  document.body.scrollTop = v;
  // Firefox
  document.documentElement.scrollTop = v;
  // Safari
  window.pageYOffset = v;
}
